/**
 * Created by wangshaohua on 2018/1/14.
 */
moduleLearningMap.controller("ContentTaskDetail", function (ExerciseContentResource, LanguageService, $$CambioConfig, UploadFileService, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService, LearningMapExerciseResource, LearningMapTaskExerciseResource, EnumService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('任务详细'));
        $scope.contentId = $stateParams.contentId;
        $scope.loadDetail();
        $scope.selectTab('tabInfo');
    });

    $scope.currentTabId = 0;
    $scope.selectTab = function (tabId) {
        if (tabId === 'tabInfo') {
            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabTask') {
            $scope.currentTabId = tabId;
        }
    };


    // ------------------------------------------------------------------------------------------------------

    $scope.loadDetail = function () {

        ExerciseContentResource.findById({id: $scope.contentId}, {}, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.currentContentTask = queryResult.data;
            UUID.format($scope.currentContentTask.tasks);
            //LoadingService.hide();
        });
    };


    $scope.save = function () {
        Alert.confirm(function () {
            ExerciseContentResource.updateTask({}, $scope.currentContentTask, function (queryResult) {
                // $log.debug("查询结果", queryResult.data);
                //$scope.learningMapCourseDetail = queryResult.data;
                Alert.success();
                //LoadingService.hide();
            });
        })
    }

    $scope.cancel = function () {
        DialogService.close();
    };

    // ------------------------------------------------------------------------------------------------------

    $scope.removeTask = function (task) {
        $scope.currentContentTask.tasks.removeForUid(task);
    }

    $scope.editTask = function (task) {
        $scope.currentTask = task;
        DialogService.open("contentTaskMgt", $scope);
    }

    $scope.addTask = function () {
        if (!$scope.currentContentTask.tasks) {
            $scope.currentContentTask.tasks = new Array();
        }
        $scope.currentTask = {
            uid: UUID.toString(),
            approve: false,
            required: true,
            sort: $scope.currentContentTask.tasks.length + 1
        }
        $scope.currentContentTask.tasks.push($scope.currentTask);
        DialogService.open("contentTaskMgt", $scope);
    }
    // ------------------------------------------------------------------------------------------------------

    $scope.taskMessage = function (task) {
        $state.go('sz.learningMapTaskMessageQuery', {
            learningMapTaskId: task.id
        });
    }

    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentTask.picture = o.id;
            });
        }
    };
});